/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.cda.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author yuanweilin
 */
public class TimeUtil {

    //获取当前时间：yyyy-MM-dd HH:mm:ss
    public static String getNowTime(){
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simple.format(new Date());
    }

    public static String getNYRTime(Date date){
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        return simple.format(date);
    }

     public static String getNYRTimeChinese(Date date){
        SimpleDateFormat simple = new SimpleDateFormat("yyyy年MM月dd日");
        return simple.format(date);
    }
    public static String getyyyyMMddHHmmss(Date date){
        SimpleDateFormat simple = new SimpleDateFormat("yyyyMMddHHmmss");
        return simple.format(date);
    }

    /**
     * @auther: zhangyingqi
     * @date: 23:13 2018/8/20
     * @param: [date, now]
     * @return: java.lang.Integer
     * @Description: 计算日期差
     */
    public static Integer getDay(Date date, Date now){
        try {
            SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd hh:mm");
            String fromDate = simple.format(now);
            String toDate = simple.format(date);
            long from = simple.parse(fromDate).getTime();
            long to = simple.parse(toDate).getTime();
            int days = (int) ((to - from)/(1000 * 60 * 60 * 24));
            return days;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * @auther: zhangyingqi
     * @date: 10:44 2018/8/23 
     * @param: [da1, da2]
     * @return: int
     * @Description: 判断是否同一天
     */
    public static int compareToDate(Date da1, Date da2){
        //将字符串格式的日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fromDate = sdf.format(da1);
        String toDate = sdf.format(da2);
        //比较两个日期
        int result= 0;
        try {
            result = sdf.parse(toDate).compareTo(sdf.parse(fromDate));
        } catch (ParseException e) {
            e.printStackTrace();
            return -2;
        }
        //如果日期相等返回0
        if(result==0){
            return 0;
        }else if(result<0){
            return -1;
        }else{
            return 1;
        }
    }

    public static void main(String args[]){
        System.out.println("Time:"+getNowTime());
    }


}
